<template>
    <div class="edit-popup">
        <popup ref="popupRef" :title="popupTitle" :async="true" width="660px" @confirm="handleSubmit"
            @close="handleClose">
            <el-form ref="formRef" :model="formData" label-width="84px" :rules="formRules">
                <el-form-item label="标题" prop="title">
                    <el-input v-model="formData.title" placeholder="请输入标题" :maxlength="100" />
                </el-form-item>
                <el-form-item label="积分" prop="integral">
                    <el-input-number style="width: 100%;" :min="0" controls-position="right"
                        v-model="formData.integral" />
                </el-form-item>
                <el-form-item label="时长" prop="duration">
                    <el-input-number style="width: 100%;" :min="0" controls-position="right"
                        v-model="formData.duration" />
                </el-form-item>
                <el-form-item label="封面" prop="picture">
                    <div>
                        <div>
                            <material-picker v-model="formData.picture" :limit="1" />
                        </div>
                        <div class="form-tips">建议尺寸：240*180px</div>
                    </div>
                </el-form-item>
                <el-form-item label="类型" prop="type">
                    <el-select v-model="formData.type" placeholder="请选择类型" >
                        <el-option label="文字" :value="1" />
                        <el-option label="视频" :value="2" />
                        <el-option label="图片" :value="3" />
                    </el-select>
                </el-form-item>
                <el-form-item label="视频" prop="video" v-if="formData.type == 2">
                    <div>
                        <div>
                            <material-picker v-model="formData.video" type="video" :limit="1" />
                        </div>
                        <div class="form-tips">建议尺寸：240*180px</div>
                    </div>
                </el-form-item>

                <el-form-item label="内容" prop="content">
                    <editor v-model="formData.content" :height="400" :width="500" />
                </el-form-item>

                <el-form-item label="学习状态">
                    <el-switch v-model="formData.status" :active-value="1" :inactive-value="0" />
                </el-form-item>
            </el-form>
        </popup>
    </div>
</template>
<script lang="ts" setup>
import type { FormInstance } from 'element-plus'

import { studyAdd, studyDetail, studyEdit } from '@/api/study'
import Popup from '@/components/popup/index.vue'

const emit = defineEmits(['success', 'close'])
const formRef = shallowRef<FormInstance>()
const popupRef = shallowRef<InstanceType<typeof Popup>>()
const mode = ref('add')
const popupTitle = computed(() => {
    return mode.value == 'edit' ? '编辑学习' : '新增学习'
})
const formData = reactive({
    id: '',
    title: '',
    integral: '',
    picture: '',
    video: '',
    duration: '',
    content: '',
    type: '',
    status: 1
})

const formRules = {
    title: [
        {
            required: true,
            message: '请输入学习标题',
            trigger: ['blur']
        }
    ],
    integral: [
        {
            required: true,
            message: '请输入积分',
            trigger: ['blur']
        }
    ],
    picture: [
        {
            required: true,
            message: '请上传图片',
            trigger: ['blur']
        }
    ],
    video: [
        {
            required: true,
            message: '请上传视频',
            trigger: ['blur']
        }
    ],
    duration: [
        {
            required: true,
            message: '请输入时长',
            trigger: ['blur']
        }
    ],
    content: [
        {
            required: true,
            message: '请输入内容',
            trigger: ['blur']
        }
    ],
    type: [
        {
            required: true,
            message: '请选择类型',
            trigger: ['blur']
        }
    ]
}

const handleSubmit = async () => {
    await formRef.value?.validate()
    mode.value == 'edit' ? await studyEdit(formData) : await studyAdd(formData)
    popupRef.value?.close()
    emit('success')
}

const open = (type = 'add') => {
    mode.value = type
    popupRef.value?.open()
}

const setFormData = (data: Record<any, any>) => {
    for (const key in formData) {
        if (data[key] != null && data[key] != undefined) {
            //@ts-ignore
            formData[key] = data[key]
        }
    }
}

const getDetail = async (row: Record<string, any>) => {
    const data = await studyDetail({
        id: row.id
    })
    setFormData(data)
}

const handleClose = () => {
    emit('close')
}

defineExpose({
    open,
    setFormData,
    getDetail
})
</script>
